package Dungeoneering.Puzzles;

import org.powerbot.game.api.methods.input.Keyboard;
import org.powerbot.game.api.methods.interactive.NPCs;
import org.powerbot.game.api.methods.tab.Inventory;
import org.powerbot.game.api.wrappers.interactive.NPC;
import org.powerbot.game.api.wrappers.node.Item;

import Dungeoneering.ActionHandlers.ActionHandler;
import Dungeoneering.Entities.Room;
import Dungeoneering.Utils.Utilities;
import Dungeoneering.api.methods.Objects;

public class MagicalConstruct extends PuzzleStrategy{

	private final int[] crate = {49543, 49544, 49545};
	private final int dormantConstruct = 11005;
	private final int magicConstruct = 11006;
	private final int stone = 17364;
	private final int bodyPart;

	public enum part{
		ARM (11002, 1, 17365, 17368, 17369),
		LEG (11004, 2, 17366, 17370, 17371),
		HEAD (11003, 3, 17367, 17372, 17373);
		
		private final int damagedId;
	    private final int sendkeyNo;
	    private final int unImbued;
	    private final int imbued1;
	    private final int imbued2;
	    
	    part(int damagedId, int sendkeyNo, int unImbued, int imbued1, int imbued2) {
	        this.damagedId = damagedId;
	        this.sendkeyNo = sendkeyNo;
	        this.unImbued = unImbued;
	        this.imbued1 = imbued1;
	        this.imbued2 = imbued2;
	    }
	    int getDamagedID() {
	        return damagedId;
	    }
	    int getSendKeyNo() {
	        return sendkeyNo;
	    }
	    int getUnimbuedID() {
	        return unImbued;
	    }
	    int getImbuedID1() {
	        return imbued1;
	    }
	    int getImbuedID2() {
	        return imbued2;
	    }
	}
	
	public enum MagicalConstructState {TAKING_ROCK, CARVING, IMBUING, REPAIRING, CHARGING, CONSTRUCT_WALKING, FINSIHED};	
	private MagicalConstructState puzzleStatus = MagicalConstructState.TAKING_ROCK;
	
	@Override
	public boolean detect(Room room) {
		if(room.containsOneOfLocations(crate))
			return true;
		return false;
	}

	@Override
	protected void loop(Room room) {
		switch(puzzleStatus.ordinal()) {
			case 0:
				Utilities.thinkSleep();
				ActionHandler.interactLocation(Objects.getNearest(crate), "Take");
				Utilities.randSleep(1500);
				if(Inventory.getCount(stone) == 0)
					puzzleStatus = MagicalConstructState.TAKING_ROCK;
				else
					puzzleStatus = MagicalConstructState.CARVING;
				break;
					
			case 1:
				Utilities.thinkSleep();
				Item lumpOfStone = null;
				Item[] items = Inventory.getItems();
				for(Item i : items) {
					if(i.getId() == stone) {
						lumpOfStone = i;
						break;
					}
				}
				
				
				
				
				
		default:
		}
		
	}
		
		/*
		
		status = Status.TRYING_SKILL_ROOM;
		
		Item[] items = Inventory.getItems();
		for(Item i : items) {
			if(i.getId() == stone) {
				lumpOfStone = i;
				break;
			}
		}
		
		switch(part) {
			case 0: while(Inventory.getCount(unfArm) == 0) {
						ActionHandler.interactInventoryItem(lumpOfStone, "Carve");
						Utilities.thinkSleep();
						Keyboard.sendKey('0');
						Utilities.randSleep(800);				
					}
					break;
			case 1: while(Inventory.getCount(unfLeg) == 0) {
						ActionHandler.interactInventoryItem(lumpOfStone, "Carve");
						Utilities.thinkSleep();
						Keyboard.sendKey('1');
						Utilities.randSleep(800);				
					}
					break;
			case 2: while(Inventory.getCount(unfHead) == 0) {
						ActionHandler.interactInventoryItem(lumpOfStone, "Carve");
						Utilities.thinkSleep();
						Keyboard.sendKey('2');
						Utilities.randSleep(800);				
					}
					break;
		}
		
		Item[] items2 = Inventory.getItems();
		for(Item i2 : items) {
			if(i2.getId() == unfArm || i2.getId() == unfLeg || i2.getId() == unfHead) {
				unImbued = i2;
				break;
			}
		}
				
		ActionHandler.interactInventoryItem(unImbued, "Imbue");
		Utilities.randSleep(800);
		
		ActionHandler.interactNpc(Npcs.getNearest(damagedConstruct[part]), "Repair");
		Utilities.randSleep(800);
		
		Npc[] npcList = Npcs.getLoaded(dormantConstruct);
		
		if(npcList.length == 0) {
			solve(room);
			return;
		}
		
		Npc[] npcList2 = null;
		
		while(npcList2.length == 0) {
			ActionHandler.interactNpc(Npcs.getNearest(dormantConstruct), "Charge");
			Utilities.randSleep(800);
			npcList2 = Npcs.getLoaded(magicConstruct);
		}
		
		//TODO Open Door
		
		status = Status.OPENING_DOOR;
		
	}*/

	@Override
	public boolean isFinished(Room room) {
		NPC[] npcList = NPCs.getLoaded(magicConstruct);
		
		if(npcList.length == 0)
			return false;
		return true;
	}
}

